package com.js.dict.controller;

import com.js.auth.client.annotation.IgnoreUserToken;
import com.js.common.controller.BaseController;
import com.js.common.response.TableResultResponse;
import com.js.dict.entity.DictType;
import com.js.dict.entity.DictValue;
import com.js.dict.service.DictValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ygl
 * @create 2018-11-05
 * @DESCRIPTION
 **/
@RestController
@RequestMapping("dictValue")
public class DictValueController extends BaseController<DictValue,String> {

    @Autowired
    private DictValueService dictValueService;

    @IgnoreUserToken
    @GetMapping("/type/{code}")
    public TableResultResponse<DictValue> getDictValueByDictTypeCode(@PathVariable("code") String code){
        Example example = new Example(DictValue.class);
        example.createCriteria().andLike("code",code+"%");
        List<DictValue> dictValues = dictValueService.selectByExample(example).stream().sorted(new Comparator<DictValue>() {
            @Override
            public int compare(DictValue o1, DictValue o2) {
                return o1.getOrderNum() - o2.getOrderNum();
            }
        }).collect(Collectors.toList());
        return new TableResultResponse<DictValue>(dictValues.size(),dictValues);
    }

    @IgnoreUserToken
    @GetMapping("/feign/{code}")
    public Map<String,String> getDictValueByCode(@PathVariable("code") String code){
        Example example = new Example(DictValue.class);
        example.createCriteria().andLike("code",code+"%");
        List<DictValue> dictValues = dictValueService.selectByExample(example);
        Map<String, String> result = dictValues.stream().collect(
                Collectors.toMap(DictValue::getValue, DictValue::getLabelDefault));
        return result;
    }
}
